#include "recorddetailtemplate.h"
//#include <QScrollArea>
//#include <QVBoxLayout>
//#include <QLabel>
//#include <QLineEdit>
//#include <QPushButton>

RecordDetailTemplate::RecordDetailTemplate()
{

}
RecordDetailTemplate::~RecordDetailTemplate()
{

}

void RecordDetailTemplate::initUi(QStringList filedsName,QList<QString> fieldsVal)
{

    QWidget * pSubWidget = new QWidget(this);
    QScrollArea *pSa = new QScrollArea(this);
    QSize dialogSize = size();

    pSa->setFixedSize(dialogSize.width(), dialogSize.height()-50);
    pSa->setWidgetResizable(true);
    pSa->setWidget(pSubWidget);
    m_editItems.clear();
    QVBoxLayout *wLayout = new QVBoxLayout();
    for (int i=0;i<filedsName.count();i++)
    {
        QHBoxLayout *eLayout = new QHBoxLayout();
        QLabel *filedLabel = new QLabel(filedsName.at(i));
        eLayout->addWidget(filedLabel,1);
        QLineEdit *valueEdit = new QLineEdit();
        if(fieldsVal.size())
            valueEdit->setText(fieldsVal.at(i));
        m_editItems.append(valueEdit);
        eLayout->addWidget(valueEdit,3);
        wLayout->addLayout(eLayout);
        if(i==0)
            valueEdit->setEnabled(false);
    }
    QSpacerItem *m_ySpacer = new QSpacerItem(20, 20, QSizePolicy::Minimum, QSizePolicy::Expanding);
    wLayout->addSpacerItem(m_ySpacer);
    pSubWidget->setLayout(wLayout);

    QHBoxLayout *bLayout = new QHBoxLayout();
    QPushButton *m_btnOk = new QPushButton(this);
    m_btnOk->setText("确认");
    QPushButton *m_btnNf = new QPushButton(this);
    m_btnNf->setText("取消");
    QSpacerItem *m_actSpacer = new QSpacerItem(20, 20, QSizePolicy::Minimum, QSizePolicy::Expanding);
    bLayout->addSpacerItem(m_actSpacer);
    bLayout->setStretch(0,8);
    bLayout->addWidget(m_btnOk,1);
    bLayout->addWidget(m_btnNf,1);
    bLayout->setContentsMargins(10, 10, 10, 10);

    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(0, 0, 0, 0);
    mainLayout->addWidget(pSa,9);
    mainLayout->addLayout(bLayout,1);

    this->setLayout(mainLayout);

    connect(m_btnOk, &QPushButton::clicked, this, &RecordDetailTemplate::slotbtnYes);
    connect(m_btnNf, &QPushButton::clicked, this, &RecordDetailTemplate::slotbtnCancel);

}


void RecordDetailTemplate::slotbtnYes()
{
    int num = m_editItems.size();
    if(num==0)
        return;
    m_valueList.clear();
    QLineEdit *le = nullptr;
    int len=0;
    for(int i=0;i<num;i++)
    {
        le=m_editItems.at(i);
        m_valueList.append(le->text());
        len+=le->text().length();
    }
    if(len==0)
        m_valueList.clear();//如果都为空，则不添加
    this->close();
}

void RecordDetailTemplate::slotbtnCancel()
{
    m_valueList.clear();
    this->close();
}
